﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace Exact_Extension
{
    public partial class MsgUpdateTaxInfo : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        ISHCMC_V_TaxInvoice TaxInvoice;
        ISHCMC_DebtorTaxInfo DebtorInfo;
        int flagUpdate = 0;
        public MsgUpdateTaxInfo(ExactDataContext _DS,ISHCMC_V_TaxInvoice _TaxInvoice)
        {
            InitializeComponent();
            DS = _DS;
            TaxInvoice = _TaxInvoice;
            if (CheckDebtorTaxInfoExisting(TaxInvoice))
            {
                this.txtTaxAddress.Text = TaxInvoice.TaxAddress;
                this.txtTaxInvoiceName.Text = TaxInvoice.TaxInvoiceName;
                this.txtVATNumber.Text = TaxInvoice.VATCode;
                DebtorInfo = this.GetDebtorTaxInfo(TaxInvoice);
                flagUpdate = 1;
            }
            else
            {
                this.txtTaxAddress.Text = TaxInvoice.TaxAddress;
                this.txtTaxInvoiceName.Text = TaxInvoice.TaxInvoiceName;
                this.txtVATNumber.Text = TaxInvoice.VATCode;
                flagUpdate = 0;
            }

        }
        private bool CheckDebtorTaxInfoExisting(ISHCMC_V_TaxInvoice TaxInv)
        {
            var query = from DebtorInfo in DS.ISHCMC_DebtorTaxInfos
                        where DebtorInfo.StudentID == TaxInv.StudentID
                        && DebtorInfo.Payercode == TaxInv.PayerCode
                        && DebtorInfo.Debnr == TaxInv.ExactStudentID
                        select DebtorInfo;
            if (query.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private ISHCMC_DebtorTaxInfo GetDebtorTaxInfo(ISHCMC_V_TaxInvoice TaxInv)
        {
            var query = from DebtorInfo in DS.ISHCMC_DebtorTaxInfos
                        where DebtorInfo.StudentID == TaxInv.StudentID
                        && DebtorInfo.Payercode == TaxInv.PayerCode
                        && DebtorInfo.Debnr == TaxInv.ExactStudentID
                        select DebtorInfo;
            return query.First();
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (flagUpdate == 0)
                {
                    ISHCMC_DebtorTaxInfo DInfo = new ISHCMC_DebtorTaxInfo
                    {
                        StudentID = TaxInvoice.StudentID,
                        Payercode = TaxInvoice.PayerCode,
                        Debnr = TaxInvoice.ExactStudentID,
                        VATNumber = this.txtVATNumber.Text,
                        TaxInvoiceAddress = this.txtTaxAddress.Text,
                        TaxInvoiceName = this.txtTaxInvoiceName.Text
                    };
                    DS.ISHCMC_DebtorTaxInfos.InsertOnSubmit(DInfo);
                    DS.SubmitChanges();
                }
                if (flagUpdate == 1)
                {
                    ISHCMC_DebtorTaxInfo DInfoUpdate = DS.ISHCMC_DebtorTaxInfos.Single(c => c.ID == DebtorInfo.ID);
                    DInfoUpdate.TaxInvoiceName = this.txtTaxInvoiceName.Text;
                    DInfoUpdate.TaxInvoiceAddress = this.txtTaxAddress.Text;
                    DInfoUpdate.VATNumber = this.txtVATNumber.Text;

                    DS.SubmitChanges();
                }
                this.DialogResult = DialogResult.Yes;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.DialogResult = DialogResult.No;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

        }

    }
}